// pages/shopManager/cardOrders/orderDetail/orderDetail.js
const common = require('../../../../utils/util.js')
const app = getApp()
var QQMapWX = require('../../../../utils/qqmap-wx-jssdk.min.js')
var qqmapsdk
Page({

    /**
     * 页面的初始数据
     */
    data: {
        userInfo: null,
        orderId: null,
        orderInfo: null,
        roleList: ['无', '普通会员', '锦鲤会员']
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        qqmapsdk = new QQMapWX({
            key: 'NZMBZ-R4OEF-E7WJS-NRKOX-FWNFS-HHFLU'
        })
        this.setData({
            orderId: options.orderId
        })
        this.getUserInfo()
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {
        let pages = getCurrentPages(); //获取当前页面实例
        let prepage = pages[pages.length - 2]
        if (prepage == undefined || prepage == null) return;
        prepage.updateData()
    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {
        this.setData({
            orderInfo: null
        })
        this.getOrderInfo()
        setTimeout(function () {
            wx.stopPullDownRefresh()
        }, 2000)
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    getUserInfo: function () {
        const that = this
        let data = {}
        common.post('User/user_info', data).then(function (res) {
            if (res.code == 204) {
                return
            } else {
                that.setData({
                    userInfo: res
                })
                that.getOrderInfo()
            }
        }).catch(function (e) {})
    },

    getOrderInfo: function () {
        const that = this
        let data = {
            goods_interval_id: this.data.orderId,
            virtual_goods: 1,
            get_user: 1,
            paramter: 1
        }
        common.post('Interval/interval_list', data).then(function (res) {
            if (res.code == 204) {
                return
            } else {
                that.setData({
                    orderInfo: res[0]
                })
            }
        }).catch(function (e) {})
    },

    phoneCall: function (e) {
        wx.makePhoneCall({
            phoneNumber: e.currentTarget.dataset.phone
        })
    },

    goNavigation: function (e) {
        const that = this
        qqmapsdk.geocoder({
            //获取表单传入地址
            address: e.currentTarget.dataset.address, //地址参数，例：固定地址，address: '北京市海淀区彩和坊路海淀西大街74号'
            success: function (res) { //成功后的回调
                const latitude = res.result.location.lat
                const longitude = res.result.location.lng
                wx.openLocation({
                    latitude,
                    longitude,
                    name: e.currentTarget.dataset.address
                })
            }
        })
    },

    goConfirm: function (e) {
        const that = this
        wx.showModal({
            title: '提示',
            content: '是否确认领取？',
            success(res) {
                if (res.confirm) {
                    wx.showLoading({
                        title: '请稍后'
                    })
                    let data = {
                        goods_interval_id: e.currentTarget.dataset.id,
                        status: 1,
                    }
                    common.post('Interval/check_interval', data).then(function (res) {
                        wx.hideLoading()
                        if (res.code == 204) {
                            wx.showModal({
                                title: '提示',
                                content: res.error,
                                showCancel: false,
                                success(res) {}
                            })
                            return
                        }
                        wx.showModal({
                            title: '提示',
                            content: '成功确认领取',
                            showCancel: false,
                            success(res) {
                                if (res.confirm) {
                                    wx.navigateBack({
                                        delta: 1
                                    })
                                }
                            }
                        })
                    }).catch(function (e) {})
                }
            }
        })
    }
})